import Vue from 'vue'
import VueRouter from 'vue-router'
/* import { component } from 'vue/types/umd' */
import Index from '../layout/Index.vue'
/* import index from '../views/Error/index.vue' */
import Login from '../views/Login'
import shouye from '../views/shouye/'
import local from '../../src/utils/local.js'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Index',
    component: Index,
    redirect: '/shouye',
    children: [
      /* 后台首页 */
      { path: '/shouye', component: shouye },
      /* 订单管理 */
      {
        path: '/dingdan',
        component: () =>
          import(/* webpackChunkName:'dingdan' */ '../views/dingdan')
      },
      /* 商品管理 */
      {
        path: '/shangping',
        component: () =>
          import(/* webpackChunkName:'shangping' */ '../views/shangping'),
        children: [
          {
            path: '/shangping/list',
            component: () =>
              import(
                /* webpackChunkName:'shangping/list' */ '../views/shangping/list.vue'
              )
          },
          {
            path: '/shangping/add',
            component: () =>
              import(
                /* webpackChunkName:'shangping/add' */ '../views/shangping/add.vue'
              )
          },
          {
            path: '/shangping/classify',
            component: () =>
              import(
                /* webpackChunkName:'shangping/classify' */ '../views/shangping/classify.vue'
              )
          }
        ]
      },
      /* 店铺管理 */
      {
        path: '/dianfu',
        component: () =>
          import(/* webpackChunkName:'/dianfu' */ '../views/dianfu')
      },
      /* 账号管理 */
      {
        path: '/zhanghao',
        component: () =>
          import(/* webpackChunkName:'/zhanghao' */ '../views/zhanghao'),
        children: [
          {
            path: '/zhanghao/list',
            component: () =>
              import(
                /* webpackChunkName:'/zhanghao/list' */ '../views/zhanghao/list.vue'
              )
          },
          {
            path: '/zhanghao/add',
            component: () =>
              import(
                /* webpackChunkName:'/zhanghao/add' */ '../views/zhanghao/add.vue'
              )
          },
          {
            path: '/zhanghao/amend',
            component: () =>
              import(
                /* webpackChunkName:'/zhanghao/amend' */ '../views/zhanghao/amend.vue'
              )
          }
        ]
      },
      /* 销售统计 */
      {
        path: '/xiaoshou',
        component: () =>
          import(/* webpackChunkName:'/xiaoshou' */ '../views/xiaoshou'),
        children: [
          {
            path: '/xiaoshou/statisticsone',
            component: () =>
              import(
                /* webpackChunkName:'/statisticsone' */ '../views/xiaoshou/statisticsone.vue'
              )
          },
          {
            path: '/xiaoshou/statisticstwo',
            component: () =>
              import(
                /* webpackChunkName:'/statisticstwo' */ '../views/xiaoshou/statisticstwo.vue'
              )
          }
        ]
      }
    ]
  },
  {
    path: '/Login',
    component: Login
  },
  {
    path: '*',
    redirect: '/404'
  },
  {
    path: '/404',
    /* component: () => import('../views/Error') */
    component: () =>
      import(/* webpackChunkName: "error" */ '../views/Error/index.vue')
  }
] /* '../views/About.vue') */
/*  component: () => import(/* webpackChunkName: "about" */

const router = new VueRouter({
  routes
})

// 使用导航守卫完成登录验证
router.beforeEach((to, from, next) => {
  const token = local.get('token')
  if (token) {
    /* 如果有token就不拦截,放过去 */
    next()
  } else {
    if (to.path === '/login') {
      /* 如果去登录就不拦截 */
      next()
    } else {
      /* 如果路径不是login就让他去login */
      next('/login')
    }
  }
})

export default router
